package com.googeioextended.youtubebetter;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.googeioextended.youtubebetter.finch.FinchVideo;

public class DetailViewActivity extends Activity {
	
	public static String CURSOR_KEY = "com.googeioextended.youtubebetter.cursorKey";
	private final static String TAG = "GoogleIO";
	private static Cursor cursor;
	private static String mediaId;
	
	private void startVideo(String videoID) {
		// default youtube app
		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
				+ videoID));
		List<ResolveInfo> list = getPackageManager().queryIntentActivities(i,
				PackageManager.MATCH_DEFAULT_ONLY);
		if (list.size() == 0) {

			Log.d("BrowseVideos", "No YouTube player");
		}
		startActivity(i);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.watch:
			startVideo(mediaId);
            return true;
        case R.id.share:
        	Intent intent = new Intent(Intent.ACTION_SEND);
        	intent.setType("text/plain");
        	String url = String.format("http://www.youtube.com/watch?v=%s&cc_load_policy=1", mediaId);
        	intent.putExtra(Intent.EXTRA_TEXT, url);
        	Intent.createChooser(intent, "Send youtube link to a friend."); 
        	startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail);
		
		final Intent intent = getIntent();

		String cursorId = intent.getStringExtra(CURSOR_KEY);
		Uri uri = Uri.parse(FinchVideo.Videos.VIDEOS_URI.toString() + "/" + cursorId);
		//FinchVideoContentProvider provider = new FinchVideoContentProvider();
		cursor = managedQuery(uri, null, null, null, null);
		cursor.moveToFirst();
		Log.d(TAG, "Cursor findings: " + cursor.toString());
		
		TextView titleView = (TextView) findViewById(R.id.title);
		String title = cursor.getString(FinchVideo.TITLE_COLUMN);
		titleView.setText(title);
		
		
		Uri thumbUri = ContentUris.withAppendedId(FinchVideo.Videos.THUMB_URI,
				cursor.getLong(FinchVideo.ID_COLUMN));
		mediaId = cursor.getString(FinchVideo.MEDIA_ID_COLUMN);
		try {
			InputStream thumbStream = getContentResolver().openInputStream(
					thumbUri);
			ImageView iv = (ImageView) findViewById(R.id.thumbnail);
			Bitmap bm = BitmapFactory.decodeStream(thumbStream);
			iv.setImageBitmap(bm);
			
			iv.setOnTouchListener(new OnTouchListener() {
				public boolean onTouch(View v, android.view.MotionEvent event) {
					Log.d(TAG, "Will play videa with id " + mediaId);
					
					startVideo(mediaId);
					return false;
				}
			});
		} catch (FileNotFoundException e) {
			Log.d(TAG, "could not open provider thumb: " + thumbUri);
		}
		// Get cursor info.
	}
	
	//static void config
}
